﻿define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Addpro, GoPage, GoDialog, $ionicModal, $stateParams) {

		$scope.input = new Object();
		$scope.input.act_id = $stateParams.act_id;
		if($scope.input.act_id) {
			$scope.title = "编辑促销";
			$scope.btnname = '保存';
		} else {
			$scope.title = "添加促销";
			$scope.btnname = '添加';
		}
		$scope.getdiscount = function() {
			$scope.part_lists = new Array();
			$scope.part_lists[0] = new Object();
			$scope.part_lists[0].name = "减固定金额";
			$scope.part_lists[0].solutions_checked = "byfixed";
			$scope.part_lists[0].isSelected = false;
			$scope.part_lists[1] = new Object();
			$scope.part_lists[1].name = "折扣出售";
			$scope.part_lists[1].solutions_checked = "topercent";
			$scope.part_lists[1].isSelected = false;
			$scope.part_lists[2] = new Object();
			$scope.part_lists[2].name = "免邮";
			$scope.part_lists[2].solutions_checked = "freeshipping";
			$scope.part_lists[2].isSelected = false;
		}
		$scope.getdiscount();

		//初始化
		$scope.init_data = function() {
			Addpro.detail($scope.input.act_id, function(data) {
				if(data.result) {
					//console.log(data.result)
					$scope.input = data.result;
					$scope.input.start_time = new Date($scope.input.start_time * 1000);//.format("yyyy-MM-dd")
					$scope.input.end_time = new Date($scope.input.end_time * 1000);
				}
				if($scope.input.act_id != null) {
					for(var i = 0; i < $scope.part_lists.length; i++) {
						if($scope.part_lists[i].solutions_checked == $scope.input.solutions_checked) {
							$scope.input.discountname = $scope.part_lists[i].name;
							break;
						}
						$scope.input.discountname = "请选择";
					}
					if($scope.input.solutions_checked == 'byfixed') {
						$scope.input.max = $scope.input.byfixed.value;
						$scope.input.byfixed_total_amount = $scope.input.byfixed.total_amount;
					}
					if($scope.input.solutions_checked == 'topercent') {
						$scope.input.max = $scope.input.topercent.value;
						$scope.input.topercent_percent = $scope.input.topercent.percent;
					}
					if($scope.input.solutions_checked == 'freeshipping') {
						$scope.input.max = $scope.input.freeshipping.value;
					}
				}
			})
		}
		$scope.init_data();
		
		$scope.clearinput = function(type) {
			if(type == 1) {
				$scope.input.name = "";
			}
			if(type == 2) {
				$scope.input.sort_order = "";
			}
		}
		//控制数字
		$scope.clearNoNum = function(obj, attr) {
			//先把非数字的都替换掉，除了数字
			obj[attr] = obj[attr].replace(/[^\d]/g, "");
		}
		//输入监听
		$scope.$watch('input.max', function(newValue, oldValue) {
			if(!newValue) {
				return
			}
			$scope.textLength = newValue.length;
			if($scope.textLength > 15) {
				//$scope.input.phone=oldValue
				$scope.input.max = newValue.substring(0, 15);
			}
		})

		//输入监听
		$scope.$watch('input.sort_order', function(newValue, oldValue) {
			if(!newValue) {
				return
			}
			$scope.textLength = newValue.length;
			if($scope.textLength > 2) {
				//$scope.input.phone=oldValue
				$scope.input.sort_order = newValue.substring(0, 2);
			}
		})
		//选择优惠方案
		$ionicModal.fromTemplateUrl("templates/modal/rebuildRole.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal = modal;
		});
		//打开
		$scope.openModal_tag = function(solutions_checked) {
			for(var i = 0; i < $scope.part_lists.length; i++) {
				if($scope.part_lists[i].solutions_checked == solutions_checked) {
					$scope.part_lists[i].isSelected = true;
				} else {
					$scope.part_lists[i].isSelected = false;
				}
			}
			$scope.modal.show();
		}
		//隐藏
		$scope.closeModal_tag = function() {
			$scope.modal.hide();
		}
		//暂时
		$scope.selectTagId = function(index) {
			for(var i = 0; i < $scope.part_lists.length; i++) {
				$scope.part_lists[i].isSelected = false;
			}
			$scope.part_lists[index].isSelected = true;
		}
		//修改优惠方案
		$scope.updatarole = function() {
			for(var i = 0; i < $scope.part_lists.length; i++) {
				if($scope.part_lists[i].isSelected) {
					$scope.input.discountname = $scope.part_lists[i].name;
					$scope.input.solutions_checked = $scope.part_lists[i].solutions_checked;
				}
			}
			$scope.closeModal_tag();

		}
		//保存前调用
		$scope.flag = true;
		$scope.savefront = function() {
			if(!$scope.input.name) {
				GoDialog.toast("请输入活动名称！");
				$scope.flag = false;
				return
			} else if(!$scope.input.start_time) {
				GoDialog.toast("请输入开始时间！");
				$scope.flag = false;
				return
			} else if(!$scope.input.end_time) {
				GoDialog.toast("请输入结束时间！");
				$scope.flag = false;
				return
			} else if(!$scope.input.sort_order) {
				GoDialog.toast("请输入优先级！");
				$scope.flag = false;
				return
			} else if(!$scope.input.max) {
				GoDialog.toast("请输入总价满多少时优惠！");
				$scope.flag = false;
				return
			} else if(!$scope.input.solutions_checked) {
				GoDialog.toast("请选择优惠方案！");
				$scope.flag = false;
				return
			}

			if(new Date($scope.input.start_time).getTime() >= new Date($scope.input.end_time).getTime()) {
				GoDialog.toast("结束时间不能小于开始时间！");
				$scope.flag = false;
				return
			}
			if($scope.input.solutions_checked == 'byfixed' && !$scope.input.byfixed_total_amount) {
				GoDialog.toast("请输入减固定价格！");
				$scope.flag = false;
				return
			}
			if($scope.input.solutions_checked == 'topercent' && !$scope.input.topercent_percent) {
				GoDialog.toast("请输入折扣！");
				$scope.flag = false;
				return
			}
			$scope.flag = true;
			$scope.input.solution = $scope.input.solutions_checked;
			$scope.input.start_time = new Date($scope.input.start_time).format("yyyy-MM-dd");
			$scope.input.end_time = new Date($scope.input.end_time).format("yyyy-MM-dd")
			/*if($scope.input.status || $scope.input.status == 'true') {
				$scope.input.status = 'true';
			} else {
				$scope.input.status = 'false'
			}*/
			if($scope.input.solutions_checked == 'byfixed') {
				$scope.input.byfixed_value = $scope.input.max;
				$scope.input.topercent_value = 0;
				$scope.input.topercent_percent = 0;
				$scope.input.freeshipping_value = 0;
			}
			if($scope.input.solutions_checked == 'topercent') {
				$scope.input.topercent_value = $scope.input.max;
				$scope.input.byfixed_value = 0;
				$scope.input.byfixed_total_amount = 0;
				$scope.input.freeshipping_value = 0;
			}
			if($scope.input.solutions_checked == 'freeshipping') {
				$scope.input.freeshipping_value = $scope.input.max;
				$scope.input.byfixed_value = 0;
				$scope.input.byfixed_total_amount = 0;
				$scope.input.topercent_value = 0;
				$scope.input.topercent_percent = 0;
			}
		}
		//保存
		$scope.save = function() {
			$scope.savefront();
			if($scope.flag) {
				Addpro.save(
					$scope.input.act_id,
					$scope.input.name,
					$scope.input.description,
					$scope.input.status,
					$scope.input.byfixed_value,
					$scope.input.byfixed_total_amount,
					$scope.input.topercent_value,
					$scope.input.topercent_percent,
					$scope.input.freeshipping_value,
					$scope.input.solution,
					$scope.input.sort_order,
					$scope.input.end_time,
					$scope.input.start_time,
					function(data) {
						if(data.success) {
							GoDialog.toast("保存成功！");
							GoPage.go_states("proorder");
						} else {
							GoDialog.toast(data.msg);
						}
					})
			}
		}
		//单击叹号显示
		$scope.show_i = function() {
			GoDialog.alert("提示","当店铺同一时段里有多个订单促销时，优先级小的促销方案优先应用");
		}

	}

	controller.$inject = ['$scope', 'Addpro', 'GoPage', 'GoDialog', '$ionicModal', '$stateParams'];

	return app.registerController("addproctrl", controller);
});